#include<iostream>
#include<string>
#include<unordered_map>
#include<fstream>
#include<unistd.h>
#include"Log.hpp"
const static std::string sep = ": ";
class Dict
{
private:
    void LoadDict(const std::string& pathname)
    {
        std::ifstream in(_pathname);
        if(!in.is_open())
        {
            LOG(FATAL,"open %s failed!\n",pathname.c_str());
            exit(1);
        }
        std::string line;
        while(std::getline(in,line))
        {
            if(line.empty()) continue;
            auto pos = line.find(sep);
            if(pos == std::string::npos) continue;
            //截取字串
            std::string key = line.substr(0,pos);
            if(key.empty()) continue;
            std::string value = line.substr(pos+sep.size());
            if(value.empty()) continue;
            //插入到字典
            _dict.insert(std::make_pair(key,value));
            LOG(DEBUG,"load %s success\n",line.c_str());
        }
        LOG(DEBUG,"load success!\n");
        in.close();
    } 
public:
    Dict(const std::string pathname):_pathname(pathname)
    {
        LoadDict(pathname);
    }
    std::string Translate(std::string word)
    {
        if(word.empty()) return "None";
        auto result = _dict.find(word);
        if(result == _dict.end()) return "None";
        else return result->second;
    }
    ~Dict()
    {

    }
private:
    std::string _pathname;
    std::unordered_map<std::string,std::string> _dict;
};